package com.flx.at.spring.code.apps.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * @Author: Fenglixiong
 * @Date: 2021/9/4 17:10
 * @Description: BeanFactoryPostProcessor运行时机：BeanDefinition都被扫描完成之后调用，可以对BeanDefinition做一些修改操作
 */
@Component
public class WorkPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("WorkPostProcessor invoke...");
        for (String name:beanFactory.getBeanDefinitionNames()){
            if("studentController".equals(name)){
                System.out.println("WorkPostProcessor Bean:["+name+"]已经被装载入Spring容器中");
                BeanDefinition beanDefinition = beanFactory.getBeanDefinition(name);
                //注释它会直接对此bean进行实例化
                //设置为懒加载，则加入容器后不会直接实例化，而是在调用的时候才回去实例化
                beanDefinition.setLazyInit(true);
            }
        }

        //修改bean信息，增加初始化函数调用
        BeanDefinition studentControllerBean = beanFactory.getBeanDefinition("studentController");
        studentControllerBean.setInitMethodName("init");
    }

}
